const errorType = require('../constants/errot-type')
const errorHandle = (error, ctx) => {
    let status, message

    switch (error.message) {
        case errorType.NAME_OR_PASSWORD_IS_REQUIRED:
            status = 400
            message = "用户名或者密码不能为空~"
            break;
        case errorType.USER_ALREADY_EXISTS:
            status = 409
            message = "用户名已经存在~"
            break;
        case errorType.USER_DOSE_NOT_EXISTS:
            status = 400
            message = "用户不存在~"
            break;
        case errorType.PASSWORD_IS_INCORRNCT:
            status = 400
            message = "密码错误~"
            break;
        case errorType.TOKEN_GEN_MISTAKE:
            status = 400
            message = "token创建失败~"
        break;
        case errorType.UNAUTHORIZATION:
            status = 401
            message = "未授权的~"
        break;
        case errorType.UNPERMISSION:
            status = 401
            message = "没有权限的~"
        break;
        default:
            status = 404
            message = "NOT FOUND"
            break;
    }

    ctx.status = status
    ctx.body = message
}

module.exports = errorHandle

